//
//  SecondPageController.m
//  SecondPage
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "SecondPageController.h"
#import "CarNavigateView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "Show360DegreeView.h"
//#import "ShowIn360ViewCtl.h"
#import "ComparisonButton.h"
#import "Module_View.h"
#import "MediaView.h"

#define AnimationDuration 0.4

@interface SecondPageController ()

@end

@implementation SecondPageController

-(id)initWithFrame:(CGRect)frame AutoInfo:(NSDictionary*)dic;
{
    self = [super init];
    if (self)
    {
        self.view.frame = frame;
//        [self.view setBackgroundColor:[UIColor whiteColor]];
        UIImageView * bgView  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [bgView setImage:[UIImage imageNamed:@"AllAuto_Image_BG.png"]];
        [self.view addSubview:bgView];
        [bgView release];

        isShowAppear =NO;
        viewDic = [[NSMutableDictionary dictionaryWithCapacity:6]retain];
        
        currentAutoID = [[dic objectForKey:@"autoid"] retain];
        
        ResourceLoader * loader = [ResourceLoader getInstance];
        NSString * replacepath = [loader get360Replace:currentAutoID];
        if (replacepath != nil)
        {
            auto360replace_view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,1024,768)];
            [self.view addSubview:auto360replace_view];
            [auto360replace_view release];
            auto360replace_view.backgroundColor = [UIColor clearColor];
            auto360replace_view.contentMode = UIViewContentModeScaleAspectFit;
            
            UIImage * image = [[UIImage alloc] initWithContentsOfFile:replacepath];
            if(image)
            {
                auto360replace_view.image = image;
                [image release];
            }
        }
        else
        {
            appearArray = [loader get360Appear:currentAutoID];
            auto360Show_view = [[Show360DegreeView alloc] initWithFrame:CGRectMake(0, 0,1024,768) sensitive:5];
            [self.view addSubview:auto360Show_view];
            [auto360Show_view release];
            [auto360Show_view loadCarData:appearArray];
        }
        
        btnAD1 = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-95, 550, 105, 52)];
        [self.view addSubview:btnAD1];
        [btnAD1 release];
        [btnAD1 setBackgroundImage:[UIImage imageNamed:@"AllAuto_Appearance_Button_Unselect"] forState:UIControlStateNormal];
        [btnAD1 setBackgroundImage:[UIImage imageNamed:@"AllAuto_Appearance_Button_Select"] forState:UIControlStateHighlighted];
        [btnAD1 setTitleColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
        [btnAD1 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btnAD1 setTitle:@"视频欣赏" forState:UIControlStateNormal];
        btnAD1.titleLabel.font = [UIFont systemFontOfSize:14];
        btnAD1.tag = 1;
        [btnAD1 addTarget:self action:@selector(onPlayVideoClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btnAD2 = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-95, 600, 105, 52)];
        [self.view addSubview:btnAD2];
        [btnAD2 release];
        [btnAD2 setBackgroundImage:[UIImage imageNamed:@"AllAuto_Appearance_Button_Unselect"] forState:UIControlStateNormal];
        [btnAD2 setBackgroundImage:[UIImage imageNamed:@"AllAuto_Appearance_Button_Select"] forState:UIControlStateHighlighted];
        [btnAD2 setTitleColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
        [btnAD2 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btnAD2 setTitle:@"功能介绍" forState:UIControlStateNormal];
        btnAD2.titleLabel.font = [UIFont systemFontOfSize:14];
        btnAD2.tag = 2;
        [btnAD2 addTarget:self action:@selector(onPlayVideoClick:) forControlEvents:UIControlEventTouchUpInside];
        

        bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 768-60,1024, 56)];
        [self.view addSubview:bottomView];
        [bottomView release];
        
        UIImageView * logoview = [[UIImageView alloc] initWithFrame:CGRectMake(bottomView.frame.size.width-309, 2, 309, 51)];
        [logoview setBackgroundColor:[UIColor clearColor]];
        logoview.image = [UIImage imageNamed:@"AllAuto_Navigation_Logo"];
        [bottomView addSubview:logoview];
        [logoview release];
        
        UIButton *  backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 56,56)];
        [backBtn setImage:[UIImage imageNamed:@"AllAuto_Button_Back"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:backBtn];
        [backBtn release];
        
        UIButton *  homeBtn = [[UIButton alloc]initWithFrame:CGRectMake(56, 0, 56,56)];
        [homeBtn setImage:[UIImage imageNamed:@"AllAuto_Button_Home"] forState:UIControlStateNormal];
        [homeBtn addTarget:self action:@selector(onHome:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:homeBtn];
        [homeBtn release];
        
        navigateView = [[CarNavigateView alloc]initWithFrame:CGRectMake(115, 0,1024-115-186, 56)];
        [bottomView addSubview:navigateView];
        [navigateView release];
        
        NSArray * moduleArray = [NSArray arrayWithObjects:Module_Key_Appearance,
                                 Module_Key_Decoration,
                                 Module_Key_Technology,
                                 Module_Key_Elaborate,
                                 Module_Key_Comparison,
                                 Module_Key_Favorite, nil];
        
        [navigateView loadData:moduleArray];


        carLogo = [[UIImageView alloc] initWithFrame:CGRectMake(40, 5, 173, 40)];
        [self.view addSubview:carLogo];
        [carLogo release];
        NSString * imagepath = [dic objectForKey:@"LogoPath"];
        [self SetCarLogo:imagepath];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onNavigate:) name:Notification_Navigate_To_CarModule object:nil];
    
    }
    return self;
}

-(void)SetCarLogo:(NSString *)imagepath
{
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:imagepath];
    [carLogo setImage:image];
    [image release];
}

-(void)onNavigate:(NSNotification*)notify
{
    [auto360Show_view sleep];
    
    NSArray * allvalue = [viewDic allValues];
    
    for (int i=0; i<[allvalue count]; i++) {
        UIView * view = [allvalue objectAtIndex:i];
        [view removeFromSuperview];
    }
    
    //为了减少内存，此处也同时释放被删除的视图
    UIView * favoriteview = [viewDic objectForKey:@"Favorite_View"];
    if (favoriteview) {
        [favoriteview retain];
    }
    UIView * comparisonview = [viewDic objectForKey:@"Comparison_View"];
    if (comparisonview) {
        [comparisonview retain];
    }
    [viewDic removeAllObjects];
    
    if (favoriteview) {
        [viewDic setObject:favoriteview forKey:@"Favorite_View"];
        [favoriteview release];
    }
    if (comparisonview) {
        [viewDic setObject:comparisonview forKey:@"Comparison_View"];
        [comparisonview release];
    }
    
    
    NSString * moduleKey = [notify object];
    
    NSString * viewName = [NSString stringWithFormat:@"%@_View",moduleKey];
    
    UIView * tempView = [viewDic objectForKey:viewName];
    if(tempView==nil)
    {
//        DFLog(@"Init View:%@ %@",viewName,currentAutoID);
        
        Class targetClass = NSClassFromString(viewName);
        tempView = [[targetClass alloc]initWithFrame:CGRectMake(0, 0, 1024, 708) autoID:currentAutoID ModuleID:moduleKey];
        
        [self.view addSubview:tempView];
        [tempView release];
        
        if (tempView) {
            [viewDic setObject:tempView forKey:viewName];
        }
        else
        {
            return;
        }
    }
    else
    {
        [self.view addSubview:tempView];
    }
    [self.view bringSubviewToFront:tempView];
    [self.view bringSubviewToFront:bottomView];
    [self.view bringSubviewToFront:carLogo];
    currentView = tempView;
}

-(void)onBack:(id)sender
{
    if (currentView)
    {
        [currentView removeFromSuperview];
        currentView = nil;
        [navigateView ClearButton];
        [auto360Show_view wakeup];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
    
}

-(void)onHome:(id)sender
{
    currentView = nil;
    [self.navigationController popViewControllerAnimated:NO];
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Navigate_FirstHomePage object:nil];
}

-(void)onPlayVideoClick:(UIControl *)sender
{
    ResourceLoader * loader = [ResourceLoader getInstance];
    //modify by xiaolong 20130904
//    NSString * videopath = [loader getADVideosPath:currentAutoID WithType:sender.tag];
//    
//    if (videopath == nil) {
//        return;
//    }
    
    NSString* videoFileName = [loader getADVideosFileName:currentAutoID type:sender.tag];
    
    if (nil == videoFileName) {
        return;
    }
    
    NSString* videopath = [NSString stringWithFormat:@"%@/Documents/paddata/video/%@", NSHomeDirectory(), videoFileName];
    if (![[NSFileManager defaultManager] fileExistsAtPath:videopath]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"资源未下载"
                                                        message:[NSString stringWithFormat:@"请到设置中下载视频资源:%@", [[videoFileName componentsSeparatedByString:@"."] objectAtIndex:0]]
                                                       delegate:nil
                                              cancelButtonTitle:@"我知道了"
                                              otherButtonTitles:nil, nil];
        [alert show];
        SafeRelease(alert);
        
        return;
    }
    
    //end modify
    
    if (mediaview == nil) {
        mediaview = [[MediaView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    }
    mediaview.alpha = 0;
    
    
    [mediaview Refresh:videopath Type:1];
    
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:mediaview];
    
    [self ShowMediaView];
}

-(void)ShowMediaView
{
    [UIView animateWithDuration:AnimationDuration
                     animations:^{
                         
                         mediaview.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         
                         
                     }];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    SafeRelease(currentAutoID);
    SafeRelease(viewDic);
    SafeRelease(mediaview);
    [super dealloc];
}

@end
